package adv8;

import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLModify2 {

	public static void main(String[] args) throws Exception {
		// 如果商品的库存量超过100，则折扣出售，discount 设置为true

		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File("products.xml"));

		List<Element> list = doc.selectNodes("/shop/product[stockcount>100]");
		if (list.size() == 0) {
			System.out.println("没有此商品");
			return;
		}

		for (Element e : list) {
			// 找到e的属性，discount,然后修改
			e.addAttribute("discount", "true");
		}
//		 
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		XMLWriter xm = new XMLWriter(new FileWriter("products.xml"), format);
		xm.write(doc);
		xm.close();// 关闭
		System.out.println("修改成功");

	}

}
